package com.acme;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.acme.AwardWinners.AwardWinnersResponseReceiver;
import com.acme.PlayerProfile.PlayerProfileResponseReceiver;
import com.acme.PlayerSearch.PlayerSearchResponseReceiver;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class FootballStatsService extends IntentService {

	
	public static final String ACTION = "action";
	
	public static final int ACTION_SEARCH = 0;
	public static final int ACTION_GET_PLAYER = 1;
	public static final int ACTION_GET_AWARDS = 2;
	public static final int ACTION_GET_WINNERS = 3;
		
	public static final String GET_PLAYER_IN_ID = "player_id";
	public static final String GET_PLAYER_OUT_JSON = "get_player_results";
	
	public static final String GET_AWARDS_IN_TYPE = "award_type";
	public static final String GET_AWARDS_OUT_JSON = "get_awards_results";
	public static final String GET_AWARDS_RESP = "com.test.intent.action.AWARDS_RECEIVED";
	
	public static final String GET_WINNERS_IN_TITLE = "award_title";
	public static final String GET_WINNERS_OUT_JSON = "get_winners_results";
	
	public static final String SEARCH_IN_QUERY = "query";
	public static final String SEARCH_OUT_JSON = "search_results";
	
	HttpClient httpclient = null;
	
	public FootballStatsService(){
		super("FootballStatsService");
		httpclient = new DefaultHttpClient();
	}

	private String getAwardWinners(String title){
		// Create a response handler
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = "";
		
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		qparams.add(new BasicNameValuePair("title", title));
		
		URI uri;
		try {
			uri = URIUtils.createURI("http", "23.21.188.51", -1, "/stats/getAwardWinners.php", 
			    URLEncodedUtils.format(qparams, "UTF-8"), null);
			HttpGet httpget = new HttpGet(uri);
			responseBody = httpclient.execute(httpget, responseHandler);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		return responseBody;
	}
	
	private String getAwards(String type){
		
		// Create a response handler
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = "";
		
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		qparams.add(new BasicNameValuePair("type", type));
		
		URI uri;
		try {
			uri = URIUtils.createURI("http", "23.21.188.51", -1, "/stats/getAwardList.php", 
			    URLEncodedUtils.format(qparams, "UTF-8"), null);
			HttpGet httpget = new HttpGet(uri);
			responseBody = httpclient.execute(httpget, responseHandler);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		return responseBody;
	}
	
	private String getPlayerWithId(String id){
		
		// Create a response handler
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = "";
		
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		qparams.add(new BasicNameValuePair("id", id));
		
		URI uri;
		try {
			uri = URIUtils.createURI("http", "23.21.188.51", -1, "/stats/getPlayerInfo.php", 
			    URLEncodedUtils.format(qparams, "UTF-8"), null);
			HttpGet httpget = new HttpGet(uri);
			responseBody = httpclient.execute(httpget, responseHandler);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		return responseBody;
	}
	
	private String getPlayersLike(String searchStr){
		
		// Create a response handler
		ResponseHandler<String> responseHandler = new BasicResponseHandler();
		String responseBody = "";
		
		List<NameValuePair> qparams = new ArrayList<NameValuePair>();
		qparams.add(new BasicNameValuePair("name", searchStr));
		URI uri;
		try {
			uri = URIUtils.createURI("http", "23.21.188.51", -1, "/stats/playerSearch.php", 
			    URLEncodedUtils.format(qparams, "UTF-8"), null);
			HttpGet httpget = new HttpGet(uri);
			responseBody = httpclient.execute(httpget, responseHandler);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		return responseBody;
	}
	
	@Override
	protected void onHandleIntent(Intent intent) {
		
		Intent broadcastIntent = new Intent();
		broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
		
		int x = intent.getIntExtra(ACTION,-1);
		Log.v("debug","Action:"+Integer.toString(x));
		
		switch(intent.getIntExtra(ACTION,-1)){
		
			case ACTION_SEARCH: 
				
				String searchStr = intent.getStringExtra(SEARCH_IN_QUERY);
				String results = getPlayersLike(searchStr);
				
				broadcastIntent.putExtra(SEARCH_OUT_JSON, results);
				broadcastIntent.setAction(PlayerSearchResponseReceiver.ACTION_RESP);
				sendBroadcast(broadcastIntent);
				
				break;
				
			case ACTION_GET_PLAYER:
				
				String id = intent.getStringExtra(GET_PLAYER_IN_ID);
				String playerResult = getPlayerWithId(id);
				
				broadcastIntent.putExtra(GET_PLAYER_OUT_JSON, playerResult);
				broadcastIntent.setAction(PlayerProfileResponseReceiver.ACTION_RESP);
				sendBroadcast(broadcastIntent);
				
				break;
				
			case ACTION_GET_AWARDS:
				
				String type = intent.getStringExtra(GET_AWARDS_IN_TYPE);
				String awardsResult = getAwards(type);
				Log.v("debug","awardsResult:"+awardsResult);
				
				broadcastIntent.putExtra(GET_AWARDS_OUT_JSON, awardsResult);
				broadcastIntent.setAction(GET_AWARDS_RESP);
				sendBroadcast(broadcastIntent);
				
				break;
				
			case ACTION_GET_WINNERS:
				
				String title = intent.getStringExtra(GET_WINNERS_IN_TITLE);
				String winnersResult = getAwardWinners(title);
				
				broadcastIntent.putExtra(GET_WINNERS_OUT_JSON, winnersResult);
				broadcastIntent.setAction(AwardWinnersResponseReceiver.ACTION_RESP);
				sendBroadcast(broadcastIntent);
				
				break;
				
			default:
				Log.v("debug","Unable to handle intent");
				
		}
		
	}

}
